package com.zlt.bkparking.shiro.realm;


import com.zlt.bkparking.entity.BackUser;
import com.zlt.bkparking.service.BackUserService;
import com.zlt.bkparking.shiro.config.JWTToken;
import com.zlt.bkparking.utils.JWTUtil;
import com.zlt.bkparking.utils.StringUtil;
import com.zlt.bkparking.vo.ActiveBackUser;
import com.zlt.bkparking.vo.PermissionVo;
import com.zlt.bkparking.vo.RoleVo;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.naming.AuthenticationException;
import java.util.List;


@Component
@Slf4j
public class UserRealm extends AuthorizingRealm {

    @Autowired
    private BackUserService backUserService;

    @Override
    public String getName() {
        return "userRealm";
    }

    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof JWTToken;
    }

    /**
     * 默认的授权方法，检查请求中是否包含权限或者角色的时候会被调用
     * @param principals
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        ActiveBackUser principal = (ActiveBackUser) SecurityUtils.getSubject().getPrincipal();
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        List<RoleVo> roleVOList = principal.getRoleVos();
        List<PermissionVo> permissionVOList = principal.getPermissionVos();
        roleVOList.forEach( r -> {
            simpleAuthorizationInfo.addRole(r.getRName());
        });
        permissionVOList.forEach( p -> {
            simpleAuthorizationInfo.addStringPermission(p.getPDesc());
                   });
        return simpleAuthorizationInfo;
    }

    /**
     * 认证方法 调用subject.login的时候会被调用
     * @param t
     * @return
     */
    @SneakyThrows
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken t) {
        String token = (String) t.getPrincipal();
        if (StringUtil.isBlank(token)){
            throw new AuthenticationException("token无效");
        }
        String username = JWTUtil.getUsername(token);
        if (StringUtil.isBlank(username)){
            throw new AuthenticationException("token无效");
        }
        BackUser backUser = backUserService.selectUserByUsername(username);
        if (backUser == null){
            throw new AuthenticationException("token无效");
        }
        if (!JWTUtil.verify(token,username,backUser.getBuPassword())){
            throw new AuthenticationException("token无效");
        }
        ActiveBackUser activeBackUser = new ActiveBackUser();
        BeanUtils.copyProperties(backUser,activeBackUser);
        // 可以查询角色和权限信息
        List<RoleVo> roleVOS = backUserService.selectRolesVoByBuId(activeBackUser.getBuId());
        activeBackUser.setRoleVos(roleVOS);
        // 可以根据角色将权限信息查询出来
        List<PermissionVo> permissionVOList = backUserService.selectPermissionVoByRoles(roleVOS);
        activeBackUser.setPermissionVos(permissionVOList);
        //查询用户的部门
//        List<DepartMentVO> departMentVOList=userService.selectDepartMentByUid(activeBackUser.getUid());
//        activeBackUser.setDepartMentVOS(departMentVOList);

        return new SimpleAuthenticationInfo(activeBackUser,token,getName());
//        return null;
    }
}
